# class Node(object):
#     def __init__(self,data=None):
#         self.data = data
#         self.next = None
# class List:
#     def __init__(self):
#         """Constructor for List"""
#         self.head = Node()
#         self.length = 0
#     def insert(self,pos,data):
#         assert pos>=0 and pos <= self.length
#         tmpPtr = self.head
#         while pos>0:
#             tmpPtr = tmpPtr.next
#             pos -= 1
#         new_node = Node()
#         new_node.data = data
#         # 将当前节点与后一个节点拆开，this_node指向后一个节点，前一个节点指向this_node
#         new_node.next = tmpPtr.next
#         tmpPtr.next = new_node
#         self.length += 1
#     def append(self,data):
#         self.insert(self.length,data)
#     def delete(self,pos):
#         assert pos<self.length
#         temPtr = self.head
#         while pos>0:
#             # 改变指针的指向
#             temPtr = temPtr.next
#             pos -= 1
#         if temPtr is not None:
#             del_next = temPtr.next
#             temPtr.next = del_next.next
#             self.length -= 1
#     def update(self,pos,data):
#         # self.delete(pos)
#         # self.insert(pos,data)
#         assert pos < self.length
#         temPtr = self.head
#         while pos > 0:
#             # 改变指针的指向
#             temPtr = temPtr.next
#             pos -= 1
#         if temPtr is not None:
#             temPtr.data = data
#     def foreach(self):
#         tmpPtr = self.head.next
#         while tmpPtr is not None:
#             yield tmpPtr.data
#             tmpPtr = tmpPtr.next
# if __name__ == '__main__':
#     l = List()
#     l.append(1)
#     l.append(2)
#     l.append(3)
#     i1 = iter(l)
#     for i in i1:
#         print(i)
#     # for i in l.foreach():
#     #     print(i)
#     # l.delete(2)
#     # for i in l.foreach():
#     #     print(i)
#     # print(l)
#     # l.update(2,5)
#     # for i in l.foreach():
#     #     print(i)
# list反转操作
#
# 这个功能在编程中经常会用到。
# 方法一
#
# >>> b = ['alpha','bete','gamma']
# >>> b[::-1]
# ['gamma', 'bete', 'alpha']
# >>> b
# ['alpha', 'bete', 'gamma']

# 双向链接linkedlist
class Node(object):
    def __init__(self,data=None):
        self.data = data
        self.next = None
        self.prev = None
class List:
    def __init__(self):
        """Constructor for List"""
        self.head = Node()
        self.length = 0
    def insert(self,pos,data):
        assert pos>=0 and pos <= self.length
        tmpPtr = self.head
        while pos>0:
            tmpPtr = tmpPtr.next
            # tmpPtr = tmpPtr.prev
            pos -= 1
        new_node = Node()
        new_node.data = data
        # 将当前节点与后一个节点拆开，this_node指向后一个节点，
        new_node.prev = tmpPtr
        new_node.next = tmpPtr.next
        tmpPtr.next = new_node
        tmpPtr.next.prev = new_node
        self.length += 1
    def append(self,data):
        self.insert(self.length,data)
    def delete(self,pos):
        assert pos<self.length
        temPtr = self.head
        while pos>0:
            # 改变指针的指向
            temPtr = temPtr.next
            pos -= 1
        if temPtr is not None:
            del_next = temPtr.next
            temPtr.next = del_next.next
            self.length -= 1
    # def update(self,pos,data):
    #     # self.delete(pos)
    #     # self.insert(pos,data)
    #     assert pos < self.length
    #     temPtr = self.head
    #     while pos > 0:
    #         # 改变指针的指向
    #         temPtr = temPtr.next
    #         pos -= 1
    #     if temPtr is not None:
    #         temPtr.data = data
    def __setitem__(self, key, value):
        assert isinstance(key,int)
        self.insert(key,value)
    def __getitem__(self,key):
        assert key >= 0 and key < self.length
        assert isinstance(key,int)
        tmpPtr = self.head.next
        while key > 0:
            tmpPtr = tmpPtr.next
            key -= 1
        return tmpPtr.data
    def __delitem__(self, key):
        assert key > 0  and key < self.length and isinstance(key,int)
        self.delete(key)
    #复写长度方法
    def __len__(self):
        return self.length
    # 实现迭代方法
    def __iter__(self):
        tmpPtr = self.head.next
        while tmpPtr is not None:
            yield tmpPtr.data
            tmpPtr = tmpPtr.next
    def __str__(self):
        return "data"+self.data
    def foreach(self):
        tmpPtr = self.head.next
        while tmpPtr is not None:
            yield tmpPtr.data
            tmpPtr = tmpPtr.next
if __name__ == '__main__':
    l = List()
    l.append(1)
    l.append(2)
    l.append(3)
    l.__setitem__(3,399)
    for i in range(0,len(l)):
        print(l[i])
    # print(len(l))
    # for i in l.foreach():
    #     print(i)
    # print("==================")
    # print(l.__getitem__(1))
    # print("==================")
    # l.__delitem__(1)
    # for i in l.foreach():
    #     print(i)
# import pickle
#
# shoplistfile = 'shoplist.data'
# # the name of the file where we will store the object
#
# shoplist = ['apple', 'mango', 'carrot']
#
# # Write to the file
#
# f = open(shoplistfile, 'wb')
#
# # help(f)
# # www.iplaypy.com
#
# pickle.dump(shoplist, f)  # dump the object to a file
# f.close()
#
# del shoplist  # remove the shoplist
#
# # Read back from the storage
# f = open(shoplistfile, 'rb')
# storedlist = pickle.load(f)
# print(storedlist)